<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity" style="padding-bottom: 0px;">
          <el-form-item label="题集名称">
            <el-input v-model="params.entity.collectTitle" placeholder="请输入题集题目"/>
          </el-form-item>
          <el-form-item label="题集描述">
            <el-input v-model="params.entity.collectDesc" placeholder="请输入题集描述"/>
          </el-form-item>
          <el-form-item label="状态">
            <!--            <el-input v-model="params.entity.statusFlag"-->
            <!--                      placeholder="请输入状态 0-停用 1-启用 2-备用"/>-->
            <el-select v-model="params.entity.statusFlag" placeholder="请选择状态" clearable="true" style="width: 100%">
              <el-option v-for="(item,index) in STATUS_LIST" :label="item.text" :value=item.label></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询</el-button>
            <el-button type="primary" :icon="Plus" @click="addDialog({}, 'add')">新增</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="500"
              :default-sort="{ prop: 'date', order: 'descending' }" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55"/>
      <!--      <el-table-column prop="id" label="" show-overflow-tooltip/>-->
      <el-table-column prop="collectTitle" label="题集题目" show-overflow-tooltip/>
      <el-table-column prop="collectDesc" label="题集描述" show-overflow-tooltip/>
      <el-table-column prop="statusFlagName" label="状态" show-overflow-tooltip/>
      <el-table-column prop="remark" label="备注" show-overflow-tooltip/>

      <el-table-column fixed="right" label="操作" width="300" show-overflow-tooltip>
        <!--        <template #header>-->
        <!--          <el-input v-model="params.search" @keyup.enter.native="searchData" placeholder="搜索角色名称/介绍"/>-->
        <!--        </template>-->
        <template #default="scope">
          <el-button link type="danger" :icon="Delete" size="small" @click="showDeleteDialog(scope.row.id)">删除
          </el-button>
          <el-button link type="primary" :icon="Edit" size="small" @click="addDialog(scope.row, 'update')">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="params.current" v-model:page-size="params.size"
                   :page-sizes="[100, 200, 300, 400]" layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </el-card>

  <el-drawer
      v-model="drawerShow"
      title="题目选项"
      :direction="'rtl'"
      size="60%"
      :before-close="handleClose">
    <depress-drawer-collect-detail :ruleForm="ruleForm"></depress-drawer-collect-detail>

    <template #footer>
      <div style="flex: auto">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="confirmClick">确定</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import {onMounted, computed, reactive, ref} from 'vue'
import {Params, Result} from "@/apis/model/common/common.interfaces"
import {Delete, Edit, Search, Share, Upload, Plus, Setting} from '@element-plus/icons-vue'
import {DepressCollect} from "@/apis/model/depress/DepressCollectModel";
import {
  deleteDepressCollectById,
  depressCollectPage,
  insertDepressCollect, updateDepressCollect
} from "@/apis/http/depress/DepressCollectHttp";
import {STATUS_LIST} from '@/util/common.util';

import DepressDrawerCollectQuestionList from "@/views/depress/DepressDrawerCollectQuestionList.vue";
import DepressDrawerDetail from "@/views/depress/DepressDrawerCollectDetail.vue";
import DepressDrawerCollectDetail from "@/views/depress/DepressDrawerCollectDetail.vue";
// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {}
})

const drawerShow = ref(false)
const ruleForm = ref<DepressCollect>({} as DepressCollect)
const addOrUpdateType = ref<string>('add' as string);

// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  depressCollectPage(params).then((res: Result) => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = (row: DepressCollect, _addOrUpdateType: string) => {
  addOrUpdateType.value = _addOrUpdateType;
  ruleForm.value = row;
  if (_addOrUpdateType == 'add') {
    ruleForm.value.selectIds = []
    ruleForm.value.collectResults = [];
  }
  drawerShow.value = true;
}

const handleClose = (done: () => void) => {
  drawerShow.value = false;
  ruleForm.value = {} as DepressCollect;
  ruleForm.value.selectIds = []
  done();
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm('是否删除?', '提醒', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    deleteDepressCollectById(id).then((res: Result) => {
      const code = res.code;
      if (code == 200) {
        ElMessage({
          message: '删除数据成功！',
          type: 'success',
        })
        initList();
      } else {
        ElMessage.error('删除数据失败！');
      }
    })
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })
}

const confirmClick = () => {
  console.log(ruleForm.value);
  ElMessageBox.confirm(
      (addOrUpdateType.value == 'add' ? '确定新增数据？': '确定修改数据？'),
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    if (addOrUpdateType.value == 'add') {
      insertDepressCollect(ruleForm.value).then((res: Result) => {
        ElMessage({
          message: '新增数据成功！',
          type: 'success',
        });
        initList()
      }).catch(err => {
        ElMessage.error('新增数据失败！')
      })
    } else if (addOrUpdateType.value == 'update') {
      updateDepressCollect(ruleForm.value).then((res: Result) => {
        ElMessage({
          message: '修改数据成功！',
          type: 'success',
        });
        initList()
      }).catch(error =>{
        ElMessage.error('修改数据失败！')
      })
    }
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })

}

onMounted(() => {
  initList()
})
</script>

<style>
</style>
